package com.innovation.chengzhangriji.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.ConfigurationManager;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.ui.widget.CuteButton;
import com.innovation.chengzhangriji.ui.widget.CuteEditGroup;
import com.innovation.chengzhangriji.ui.widget.CuteTimerButton;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;
import com.innovation.library.util.InnovationAlgorithm;
import com.jiuan.oa.android.library.util.MobileNumberUtil;

public class ForgetActivity extends BaseActivity implements View.OnClickListener, CuteTimerButton.TimerListener, CuteToolbar.OnClickCuteToolbarListener {

    private String second;

    private CuteTimerButton btnTimer;

    private CuteEditGroup editNumber;

    private CuteEditGroup editCode;

    private CuteEditGroup editPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        second = getString(R.string.button_timer);
        setContentView(R.layout.activity_forget);
        CuteToolbar toolbar = (CuteToolbar) findViewById(R.id.toolbar);
        toolbar.setOnClickCuteToolbarListener(this);
        editNumber = (CuteEditGroup) findViewById(R.id.edit_text_number);
        editCode = (CuteEditGroup) findViewById(R.id.edit_text_code);
        btnTimer = (CuteTimerButton) findViewById(R.id.button_timer);
        btnTimer.setText("获取验证码");
        btnTimer.setTimerListener(this);
        editPassword = (CuteEditGroup) findViewById(R.id.edit_text_password);
        CuteButton btnReset = (CuteButton) findViewById(R.id.btn_reset);
        btnReset.setOnClickListener(this);
    }

    @Override
    public void timerStart(View v, int startCount) {
        if (v.getId() == R.id.button_timer) {
            btnTimer.setText(startCount + second);
            String code = editNumber.getContent();
            if (checkNumber(code)) {
                btnTimer.setTextColor(Color.LTGRAY);
                requestResetGetCode(code);
            } else {
                stopTime();
            }
        }
    }

    @Override
    public void timerCount(View v, int currentCount) {
        if (v.getId() == R.id.button_timer) {
            btnTimer.setText(currentCount + second);
        }
    }

    @Override
    public void timerEnd(View v) {
        if (v.getId() == R.id.button_timer) {
            btnTimer.setTextColor(Color.BLACK);
            btnTimer.setText("再次获取");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_reset:
                resetPassword();
                break;
        }
    }

    private void resetPassword() {
        String number = editNumber.getContent();
        String code = editCode.getContent();
        String password = editPassword.getContent();
        if (checkInput(number, code, password)) {
            requestReset(number, code, password);
        }
    }

    private boolean checkNumber(String number) {
        if (!MobileNumberUtil.isMobile(number)) {
            tipShow(R.string.tip_error_number);
            return false;
        }
        return true;
    }

    private boolean checkInput(String number, String code, String password) {
        if (!checkNumber(number)) {
            return false;
        }
        if (TextUtils.isEmpty(code)) {
            tipShow(R.string.tip_error_code_empty);
            return false;
        }
        if (TextUtils.isEmpty(password)) {
            tipShow(R.string.tip_error_password_need);
            return false;
        }
        return true;
    }

    public void stopTime() {
        btnTimer.stop();
    }

    private void requestResetGetCode(final String phone) {
        // 当未连接网络,或网络连接超时的情况发生时,重置计时器.
        Client.requestResetGetCode(this, phone, new ResponseHandler() {

            @Override
            public void onInnovationSuccess(JsonElement value) {
                super.onInnovationSuccess(value);
                tipShow("验证码发送成功");
            }

            @Override
            public void onInnovationExceptionFinish() {
                super.onInnovationExceptionFinish();
                stopTime();
            }

        }, new Client.NetworkUnavailableListener() {
            @Override
            public void networkUnavailable() {
                tipShow(R.string.tip_network_unavailable);
                stopTime();
            }
        });
    }

    private void requestReset(String number, String code, String password) {
        password = InnovationAlgorithm.SHA1(Config.getConfig(ConfigurationManager.PASSWORD_SALT), password);
        Client.requestReset(this, number, code, password, new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {
                tipShow("重置密码成功");
                finish();
            }

            @Override
            public void onChZhRJFailure(String value) {
                tipShow(value);
            }

            @Override
            public void onInnovationExceptionFinish() {
                dismissProgressDialog();
            }

            @Override
            public void onInnovationFinish() {
                dismissProgressDialog();
            }

        }, networkUnavailableSampleImpl());
    }

    @Override
    public void onClickLeft() {
        finish();
    }

    @Override
    public void onClickRight() {

    }
}
